<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <style>
            .avatar-uploader .el-upload {
                border: 1px dashed #d9d9d9;
                border-radius: 6px;
                cursor: pointer;
                position: relative;
                overflow: hidden;
            }
            .avatar-uploader .el-upload:hover {
                border-color: #409EFF;
            }
            .avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 178px;
                height: 178px;
                line-height: 178px;
                text-align: center;
            }
            .avatar {
                width: 178px;
                height: 178px;
                display: block;
            }
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">

            <div class="content-header">
                <h1>系统管理<small>菜单管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                    <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <el-button type="primary" class="butT" @click="addMenu()">添加一级菜单</el-button>
                    <el-tree
                        :data="menuData"
                        node-key="id">
                              <span class="custom-tree-node" slot-scope="{ node, data }">
                                <span>{{ node.label }}</span>
                                  <el-button type="text" size="mini" @click="addChildren(node,data)">添加子菜单</el-button>
                              </span>
                     </el-tree>
                </div>


                <div id="addMenuDiv">
                    <el-dialog title="新增菜单" :visible.sync="dialogAddMenuVisible">
                        <el-form :model="menu">
                            <el-form-item label="菜单名称" :label-width="formLabelWidth">
                                <el-input v-model="menu.name" ></el-input>
                            </el-form-item>
                            <el-form-item label="跳转地址" :label-width="formLabelWidth">
                                <el-input v-model="menu.linkUrl" ></el-input>
                            </el-form-item>
                            <el-form-item label="图标" :label-width="formLabelWidth">
                                <el-input v-model="menu.icon" ></el-input>
                            </el-form-item>

                        </el-form>
                        <div slot="footer" class="dialog-footer">
                            <el-button @click="dialogAddMenuVisible = false">取 消</el-button>
                            <el-button type="primary" @click="handlerAddMenu()">确 定</el-button>
                        </div>
                    </el-dialog>
                </div>

            </div>
        </div>
    </body>

    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                menuData:[],
                dialogAddMenuVisible:false,
                formLabelWidth: '120px',
                menu:{
                    name:'',
                    linkUrl:'',
                    parentMenuId:null
                }
            },
            created() {
                this.loadMenu();
            },
            methods: {
                //添加子菜单
                addChildren(node,data){
                    this.menu = {}
                    this.dialogAddMenuVisible = true;
                    //将当前菜单id作为要添加菜单的父亲
                    this.menu.parentMenuId =  data.id;
                },
                //弹出添加菜单框
                addMenu(){
                    //重置表单
                    this.menu = {}
                    this.dialogAddMenuVisible = true;
                },
                //菜单数据保存到数据库
                handlerAddMenu(){
                    axios.post("/menu/add.do",this.menu).then((res)=>{
                        this.dialogAddMenuVisible = false;
                        this.$message({
                            message:res.data.message,
                            type:res.data.flag ? "success" : "error"
                        });
                        //刷新页面
                        this.loadMenu();
                        this.menu.parentMenuId = null;
                    });

                },
                loadMenu(){
                    axios.get("/menu/getMenus.do?id=0").then((res)=>{
                        this.menuData = res.data.data.menus;
                    })
                }
            }
        })
    </script>
</html>
